package groups.invertebrates.gui;
import groups.invertebrates.InvertebrateVectorOfAgent;
import groups.invertebrates.dao.IInvertebrateVectorOfAgentDao;
import groups.invertebrates.dao.JDBCInvertebrateVectorOfAgentDao;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Vector;

import javax.swing.JButton;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.application.Application;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class InvertebrateVectorOfAgentGUI extends javax.swing.JFrame {
	private JPanel topPanelEast;
	private JPanel topPanelSouth;
	private JPanel topPanelWest;
	private Connection con;
	

	
	private JTable fullTable;
	
	private JTextField carriedEtiologicalAgentInput;
	private JTextPane carriedEtiologicalAgent;
	
	private JTextPane vectorInvetebrateSpecies;
	private JTextField vectorInvetebrateSpeciesInput;
	
	private JButton updateButton;
	private JButton deleteButton;
	private JButton searchButton;
	private JButton insertButton;
	private JPanel topPanelCenter;
	/**
	* Auto-generated main method to display this JFrame
	*/
		
	public InvertebrateVectorOfAgentGUI(Connection con) {
		super("InvertebrateVectorOfAgent");
		this.con=con;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				topPanelEast = new JPanel();
				getContentPane().add(topPanelEast, BorderLayout.EAST);
				topPanelEast.setPreferredSize(new java.awt.Dimension(110, 280));
				{
					insertButton = new JButton();
					topPanelEast.add(insertButton);
					insertButton.setName("insertButton");
					insertButton.setPreferredSize(new java.awt.Dimension(105, 25));
					insertButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							System.out.println("insertButton.actionPerformed, event="+evt);
							insertAction();
						}
					});
				}
				{
					searchButton = new JButton();
					topPanelEast.add(searchButton);
					searchButton.setName("searchButton");
					searchButton.setPreferredSize(new java.awt.Dimension(105, 25));
					searchButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							System.out.println("searchButton.actionPerformed, event="+evt);
							searchAction();
						}
					});
				}
				{
					updateButton = new JButton();
					topPanelEast.add(updateButton);
					updateButton.setName("updateButton");
					updateButton.setPreferredSize(new java.awt.Dimension(105, 25));
					updateButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							System.out.println("updateButton.actionPerformed, event="+evt);
							updateAction();
						}
					});
				}
				{
					deleteButton = new JButton();
					topPanelEast.add(deleteButton);
					deleteButton.setName("deleteButton");
					deleteButton.setPreferredSize(new java.awt.Dimension(105, 25));
					deleteButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							System.out.println("deleteButton.actionPerformed, event="+evt);
							deleteAction();
						}
					});
				}
			}
			{
				topPanelSouth = new JPanel();
				getContentPane().add(topPanelSouth, BorderLayout.SOUTH);
				topPanelSouth.setPreferredSize(new java.awt.Dimension(408, 152));
				{
					TableModel fullTableModel = 
						new DefaultTableModel(
								getTableContents(),
								new String[] { "ID", "Nome", "Clima", "Vegetacao" });
					fullTable = new JTable();
					topPanelSouth.add(fullTable);
					fullTable.setModel(fullTableModel);
					fullTable.setPreferredSize(new java.awt.Dimension(398, 142));
					fullTable.setName("fullTable");
				}
			}
			{
				topPanelCenter = new JPanel();
				getContentPane().add(topPanelCenter, BorderLayout.CENTER);
				{
					vectorInvetebrateSpeciesInput = new JTextField();
					topPanelCenter.add(vectorInvetebrateSpeciesInput);
					vectorInvetebrateSpeciesInput.setPreferredSize(new java.awt.Dimension(200, 20));
				}
				{
					carriedEtiologicalAgentInput = new JTextField();
					topPanelCenter.add(carriedEtiologicalAgentInput);
					carriedEtiologicalAgentInput.setPreferredSize(new java.awt.Dimension(200, 20));
				}
			
			}
			{
				topPanelWest = new JPanel();
				getContentPane().add(topPanelWest, BorderLayout.WEST);
				topPanelWest.setPreferredSize(new java.awt.Dimension(80, 280));
				{
					vectorInvetebrateSpecies = new JTextPane();
					topPanelWest.add(vectorInvetebrateSpecies);
					vectorInvetebrateSpecies.setName("vectorInvetebrateSpecies");
					vectorInvetebrateSpecies.setText("VectorInvetebrateSpecies");
					vectorInvetebrateSpecies.setPreferredSize(new java.awt.Dimension(80, 20));
				}
				{
					carriedEtiologicalAgent = new JTextPane();
					topPanelWest.add(carriedEtiologicalAgent);
					carriedEtiologicalAgent.setName("carriedEtiologicalAgent");
					carriedEtiologicalAgent.setText("Number of segments");
					carriedEtiologicalAgent.setPreferredSize(new java.awt.Dimension(80, 20));
				}
				
			}
			pack();
			this.setSize(418, 310);
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(getContentPane());
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private String[][] getTableContents(){
		IInvertebrateVectorOfAgentDao invertebrateVectorOfAgentDao = new JDBCInvertebrateVectorOfAgentDao(con);
		Vector<InvertebrateVectorOfAgent> countries = invertebrateVectorOfAgentDao.readAll();
		String[][] table = new String[countries.size()][5];
		for (int i=0; i < countries.size(); i++){
			//table[i][0] = String.valueOf(countries.get(i).getIdInvertebrateVectorOfAgent());
			table[i][0] = String.valueOf(countries.get(i).getVectorInvetebrateSpecies());
			table[i][1] = String.valueOf(countries.get(i).getCarriedEtiologicalAgent());
			
		}
		return table;
	}
	
	private void insertAction(){
		IInvertebrateVectorOfAgentDao invertebrateVectorOfAgentDao = new JDBCInvertebrateVectorOfAgentDao(con);
		invertebrateVectorOfAgentDao.create(Integer.parseInt(vectorInvetebrateSpeciesInput.getText()), Integer.parseInt(carriedEtiologicalAgentInput.getText()));
		refreshTable();
	}

	private void searchAction(){
	}
	
	private void updateAction(){
		int rowIndice = fullTable.getSelectedRow();
		int id = Integer.parseInt((String) fullTable.getValueAt(rowIndice, 0));
		IInvertebrateVectorOfAgentDao invertebrateVectorOfAgentDao = new JDBCInvertebrateVectorOfAgentDao(con);
		invertebrateVectorOfAgentDao.update(id, Integer.parseInt(vectorInvetebrateSpeciesInput.getText()), Integer.parseInt(carriedEtiologicalAgentInput.getText()));
		refreshTable();
	}

	private void deleteAction(){
		int rowIndice = fullTable.getSelectedRow();
		int id = Integer.parseInt((String) fullTable.getValueAt(rowIndice, 0));
		IInvertebrateVectorOfAgentDao invertebrateVectorOfAgentDao = new JDBCInvertebrateVectorOfAgentDao(con);
		invertebrateVectorOfAgentDao.delete(id);
		refreshTable();
	}
	
	private void refreshTable(){
		topPanelSouth.remove(fullTable);
		TableModel fullTableModel = 
			new DefaultTableModel(
					getTableContents(),
					new String[] { "ID", "VectorInvetebrateSpecies", "CarriedEtiologicalAgent", "Sting", "Colors" });
		fullTable = new JTable();
		topPanelSouth.add(fullTable);
		fullTable.setModel(fullTableModel);
		fullTable.setPreferredSize(new java.awt.Dimension(398, 142));		
	}
}
